<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Category extends CI_Controller {
	private $pageData=array();
	private $siteData=array();
	private $categoryName;
	private $tableName;
	private $categoryTableName;
	private $pageSize=20;
	public function __construct(){
		parent::__construct();
		//连接数据库
		$this->db = $this->load->database ( "default", TRUE );
		//读取dilicms的配置属性
		$this->siteData = $this->db->get($this->db->dbprefix('site_settings'))->row_array();
		$this->tableName=$this->db->dbprefix('u_m_content');
		$this->categoryTableName=$this->db->dbprefix('u_c_category');
		
		
	}
	public function index($id=1){
		//后台设置后缀为空，否则分页出错
		$this->config->set_item('url_suffix', '');
		//载入分页类
		$this->load->library('pagination');
		$id=$this->input->get("id");
		$_page=$this->input->get("page");
		$_page=empty($_page)?1:$_page;
		if(!is_numeric($id)&&!is_numeric($_page)){
			exit ("参数非法");
		}
		$this->categoryName=$this->__getCategoryName($id);
		if($this->categoryName==-1){
			exit( "该栏目不存在！");
		}
		else{
			$this->pageData["id"] = $id;
			$this->pageData["title"]=$this->categoryName;
			$this->pageData["tongzhi"]=$this->__getAside(26);
			$this->pageData["center_new"]=$this->__getAside(12);
			$pageArr = $this->getPageData($_page,$id);
			$this->pageData['pageArr'] = $pageArr;
			$this->pageData["currentList"]=$this->__getListDatas($id,$pageArr['pageSize'],$pageArr['pageMax']);
			
			$this->load->view('category',$this->pageData);
		}
	}
	//获取边栏
	private function __getAside($_id){
		$whereCondition="where ";
		$whereCondition.=($_id!=0)?("category_name={$_id}"):("1=1");
		$sql="select * from {$this->tableName} {$whereCondition}";
		$sql.=" order by id desc limit 0,1";
		$resData=$this->db->query($sql)->result_array();
		return $resData;
	}
	
//	//分页数据
//	private function __getPagesData($_id,$_page){
//		$whereCondition="where ";
//		$whereCondition.=($_id!=0)?("category_name={$_id}"):("1=1");
//		$sql="select count(id) as pageMaxSize from {$this->tableName} {$whereCondition}";
//		$resData=$this->db->query($sql)->row_array();
//		$pageMaxSize=$resData["pageMaxSize"];
//		$pageSize=$this->pageSize;
//		$pages=ceil($pageMaxSize / $pageSize);
//		$pageMax=($_page-1)*$pageSize;
//		$pagesArr=array(
//				"pageSize"=>$pageSize,//每页多少条记录
//				"pages"=>$pages,//共多少页
//				"page"=>$_page,//当前页数
//				"pageMax"=>$pageMax//查询的起始索引
//		);
//		return $pagesArr;
//	}
	public function getPageData($page,$id){
		$sql = "select count(id) as pageMaxSize from {$this->tableName} where category_name = {$id}";
		$resData = $this->db->query($sql)->row_array();
		$pageMaxSize = $resData['pageMaxSize'];
		$pageSize = $this->pageSize;
		$pages = ceil($pageMaxSize/$pageSize);
		$pageMax = ($page-1)*$pageSize;
		$pagesArr=array(
				"pageMaxSize"=>$pageMaxSize,
				"pageSize"=>$pageSize,//每页多少条记录
				"pages"=>$pages,//共多少页
				"page"=>$page,//当前页数
				"pageMax"=>$pageMax//查询的起始索引
		);
		return $pagesArr;
	}
	
	//获取当前所有新闻列表
	private function __getListDatas($_id,$pageSize,$pageMax){
		$whereCondition="where ";
		$whereCondition.=($_id!=0)?("category_name={$_id}"):("1=1");
		$sql="select id,title,create_time,category_name from {$this->tableName} {$whereCondition}";
		$sql.=" order by id desc ";
		$sql.="limit {$pageMax},{$pageSize}";
		$resData=$this->db->query($sql)->result_array();
		return $resData;
	}
	
	//获取栏目名称
	private function __getCategoryName($id){
		$whereCondition="where classid  = {$id}";
		$sql="select category_name from {$this->categoryTableName}  {$whereCondition}";
		$resData=$this->db->query($sql)->row_array();
		$categoryName=(sizeof($resData)==0)?-1:$resData["category_name"];
		return $categoryName;
	}
	
	
}